stars.admin = liwe.module ( 'stars.admin' );

this._dest_div = 'block_main';
stars.admin.modules_lst = [];
stars.admin.admin_man = null;

stars.admin.set_dest_div = function ( div_id )
{
	this._dest_div = div_id;
};
/* --- ADMIN MANAGER FUNCTIONS --- */
stars.admin._admin_man_init = function ( dest_div )
{
        if ( stars.admin.admin_man ) return;
        stars.admin.admin_man = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'list_action': "stars.ajax.list",
                                        'del_action':  "stars.admin.delete",
                                        'mod_menu_actions': [],
					'filter_form' : stars.admin._filter_module,
					'filter_form_name' : "COM-mods-avail"
                                } );

        stars.admin.admin_man.set_row_fields (
                                [
                                        [ "id", "id" ],
					[ "rank", "rank" ],
                                        [ "module", "module" ],
                                        [ "login", "login" ]
                                ]
                                );

        // stars.admin.admin_man.templates ['table_start' ] = stars.admin.templates [ 'table_start' ];
        // stars.admin.admin_man.templates [ 'table_row' ] = stars.admin.templates [ 'table_row' ];

        stars.admin.admin_man.cbacks [ 'row_manip' ] = stars.admin._row_manip;
        stars.admin.admin_man.cbacks [ 'del_item' ]  = function ( id ) { stars.admin.del ( id ); };

};

stars.admin._row_manip = function ( ds, row )
{
	row [ "_id" ] = row [ 'id' ];
	console.debug ( row );
};

stars.admin.list = function ( dest_div, module )
{
	stars.admin.admin_man.show_list ( dest_div );
};

stars.admin._filter_module = function ( dest_div )
{
	if ( stars.admin.modules_lst.lenght < 1 ) return;
	var opts = [];
	stars.admin.modules_lst.iterate ( function ( v ) 
				{
				 	opts.push ( { label : v [ 'module' ], value : v [ 'module' ] } );
				} );
	var f = new liwe.form.instance ( 'STARS-mods-avail' );
	f.select (  { name : 'module', label : 'Moduli', options: opts, force_select: true, onchange : "stars.admin._filter_apply ();" } );
	f.set ( dest_div );	
};

stars.admin._filter_apply = function ()
{
	stars.admin.admin_man.search_start ();
};
 
stars.admin.init = function ()
{
	stars.admin.ajax ( { action : 'stars.admin.get_modules', id_site : site._id_site } , function ( data ) 
			{
				if ( data [ 'modules' ] ) stars.admin.modules_lst = data [ 'modules' ]; 
				var _acts = [
						{ label: 'Lista stars.', func: function () { stars.admin.list ( 'system-content' ); } },
						{ label: 'Aggiungi stars.', func: function () { stars.admin.set_dest_div ( 'system-content' ); 
												  stars.admin.add () } }
					    ];
				stars.admin.register ( _acts );
				stars.admin._admin_man_init ( 'system-content' );	
			} );
};
